<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\OrderAudit;
use App\Models\OrderAuditLog;
use App\Rep\OrderChildRep;
use App\Rep\SemiFinishProductRep;
use Illuminate\Console\Command;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     * 控制台命令的名称和签名。
     * php artisan addtestdata:orderchild
     * @var string
     */
    protected $signature = 'addtestdata:orderchild';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '添加测试子订单数据';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $order_list = Order::all();
        foreach($order_list as $k=>$v){
            $this->info('===id='.$v->id.'开始===');
            //是否是补录订单
            if($v->is_replenishment == 1){
                $this->info('补录订单，加入成品库');
                //补录订单，直接加入成品库
                $seniFinishProductRep = new SemiFinishProductRep();
                $seniFinishProductRep->replenishmentAddFinishProductRep($v,3,1,1);
                $v->status = 1;//审核状态：通过
                $v->production_status = 3;//生产状态：成品
                $v->save();
            }else{
                $this->info('判断审核流程');
                //订单审核
                //判断审核流程，是否对订单进行审核
                $orderAuditList = self::judgeOrderAudit();
                if($orderAuditList){
                    $this->info('需要审核');
                    //添加审核记录
                    self::createOrderAudit($orderAuditList,$v->id,1,'15135970424',1,'超级管理员','自动添加的测试备注');
                }else{
                    $this->info('不需要审核');
                    //审核流程为空,订单直接过审
                    $v->status = 1;
                    $v->production_status = 1;//生产状态：生产中
                    $v->save();
                }
                //自动生成子订单
                $this->info('生成子订单');
                $childOrderRep = new OrderChildRep();
                $childOrderRep->autoCreateOrderChild($v->product_id,$v->id);
            }
        }
        return Command::SUCCESS;
    }

    static function createOrderAudit($list,$order_id,$user_id,$user_name,$role_id,$role_name,$remark){
        foreach($list as $k=>$v){
            //添加审核记录
            OrderAuditLog::Create([
                'order_id'=>$order_id,
                'user_id'=>$user_id,
                'user_name'=>$user_name,
                'role_id'=>$role_id,
                'role_name'=>$role_name,
                'order_audit_name'=>$v->field_name,
                'audit_name'=>$v->audit_name,
                'order_audit_id'=>$v->id,
                'res'=>0,
                'remark'=>$remark??''
            ]);
        }
    }

    static public function getOrderAuditFlowRep(){
        return OrderAudit::where('status',1)->orderBy('sort','desc')->get();
    }

    static function judgeOrderAudit(){
        $orderAuditList = self::getOrderAuditFlowRep();
        if($orderAuditList->count()){
            return $orderAuditList;
        }
        return false;
    }
}


